package com.example.shopping;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.example.shopping.goods.GoodsTypeBean;
import com.example.shopping.utils.BaseActivity;
import com.example.shopping.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener {
    TextView nameTv,priceTv,desTv,numTv;
    Button buyBtn,cartBtn;
    ImageView backIv,bigIv;
    GridView likeGv;
    List<GoodsTypeBean.DataBean> data;    //数据源
    private LikeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        initView();
        //接受上一页页面传来的数据
        GoodsTypeBean.DataBean bean = (GoodsTypeBean.DataBean) getIntent().getSerializableExtra("info");
        showView(bean);
        connectNetwork(URLUtils.guessLikeURL); //联网
        setItemClick();
    }
    /* 猜你喜欢每一项的点击事件*/
    private void setItemClick() {
        likeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsTypeBean.DataBean bean = data.get(position);
                Intent intent = new Intent(GoodsInfoActivity.this, GoodsInfoActivity.class);
                intent.putExtra("info",bean);
                startActivity(intent);
//                finish();
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        GoodsTypeBean bean = new Gson().fromJson(result, GoodsTypeBean.class);
        List<GoodsTypeBean.DataBean> list = bean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();
    }

    /* 将数据显示在控件上*/
    private void showView(GoodsTypeBean.DataBean bean) {
        nameTv.setText(bean.getName());
        priceTv.setText("￥ "+bean.getPrice()+"元");
        desTv.setText(bean.getInfo());
        numTv.setText("剩余: "+bean.getCount()+"件");
        String picUrl = URLUtils.PUBLIC_URL+bean.getPhoto();
        Picasso.with(this).load(picUrl).into(bigIv);
        // 展示GridView的内容
        data = new ArrayList<>();
        // 适配器
        adapter = new LikeAdapter(this, data);
        likeGv.setAdapter(adapter);
    }

    //    初始化控件
    private void initView() {
        nameTv = findViewById(R.id.info_tv_name);
        priceTv = findViewById(R.id.info_tv_price);
        desTv = findViewById(R.id.info_tv_des);
        numTv = findViewById(R.id.info_tv_num);
        buyBtn = findViewById(R.id.info_btn_buy);
        cartBtn = findViewById(R.id.info_btn_cart);
        backIv = findViewById(R.id.info_iv_back);
        bigIv = findViewById(R.id.info_iv_goods);
        likeGv = findViewById(R.id.info_gv_like);
        //设置点击事件
        buyBtn.setOnClickListener(this);
        cartBtn.setOnClickListener(this);
        backIv.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.info_btn_buy:

                break;
            case R.id.info_btn_cart:

                break;
            case R.id.info_iv_back:
                finish();   //销毁当前页面
                break;
        }
    }
}
